import configparser
import os

from utils.path_util import conf_path


class UtilIni:
    def __init__(self):
        self.path = os.path.join(conf_path, "config.ini")
        self.config = configparser.ConfigParser()
        self.config.read(self.path, encoding="utf8")

    def getvalue(self, section, option):
        """
        从ini文件中获取数据
        :param section: 类
        :param option: 键
        :return:
        """
        try:
            return self.config[section][option]
        except Exception as e:
            # Log().error(f"读取配置文件错误，{e}")
            print(e)
            raise e

    def writeln(self, section, option, value):
        """
        往ini文件写入输入
        :param section: 类
        :param option: 键
        :param valuetext: 值
        :return:
        """
        try:
            self.config.set(section, option=option, value=value)  # 注意这里的selection一定要先存在！
            with open(self.path, "w+", encoding="UTF-8") as f:
                self.config.write(f)
        except Exception as e:
            raise e
